import { _decorator, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { Rank } from '../entity/Rank';
import { MapZone } from '../entity/MapZone';
import { UIManager } from '../common/UIManager';
import { RankDetail } from './RankDetail';
import { PrefabEnum } from '../common/PrefabEnum';
import { Utils } from '../common/Utils';
import { UserInfo } from '../common/UserInfo';
const { ccclass, property } = _decorator;

@ccclass('VillageItem')
export class VillageItem extends Component {

    @property(Label)
    index:Label
    @property(Label)
    village:Label
    @property(Label)
    playerName:Label
    @property(Label)
    people:Label
    @property(Label)
    coord:Label
    @property(SpriteFrame)
    activeBg:SpriteFrame

    private rank:Rank
    start() {
        this.playerName.node.on(Node.EventType.TOUCH_END, this.onTouch, this)
    }

    setData(rank:Rank) {
        this.rank = rank

        this.index.string = rank.VillageRank + ''
        this.village.string = rank.VillageName
        this.playerName.string = rank.PlayerName
        this.people.string = rank.VillagePeople + ''

        Utils.handleEllipsis(this.playerName.node)
        Utils.handleEllipsis(this.village.node)

        let xy = MapZone.getXyByZoneId(rank.ZoneId)
        this.coord.string = `(${xy[0]}|${xy[1]})`

        if(this.rank.PlayerId == UserInfo.instance.villageData.uid){
            this.getComponent(Sprite).spriteFrame = this.activeBg
        }
    }

    async onTouch(){
        if(!this.rank){
            return
        }

        let ui = await UIManager.instance.openUI(PrefabEnum.RankDetail)
        ui.getComponent(RankDetail).setData(this.rank.PlayerId)
    }
}


